import { pointsAPi, meberDetailApi, logisticsAPi,OrderTrackAPi} from './api';
import { Toast } from 'antd-mobile';
import {collectionListApi} from "../collectionList/api";




export default {
    namespace: 'member',
    state: {
        pointsList:[],
        hasMore:true,
        OrderTrack:[],
        filter:{
            searchType:"",
            keyword:"",
            pageNo:1,
            brandId:"",
            areaId:"",
            specFilter:"",
            sortField:"",
            sortOrder:"",
            maximumPrice:"",
            minimumPrice:"",
            identification:"1",
            storeId:""
        },
        logisticsList: [],
        logisticsData: {},
    },
    reducers:{
        save(state, { data }) {
            return { ...state, ...data };
        },
    },
    effects: {
        *pointsEFF({ obj}, { put, call,select }) {
            Toast.loading('加载中...', 3);
            const pointslist = yield select((state)=>state.member);
            const result = yield call(pointsAPi,obj);
            if (result.code == 200) {
                if( result.ret !='' ){
                    yield put({ type: 'save', data: {pointsList:result.ret || [],hasMore:result.ret.list.length==20,filter:{...pointslist.filter}} });
                    Toast.hide();
                }else {
                    yield put({ type: 'save', data: { hasMore: false }});
                }
                return { r:true };
            } else {

            }
        },
        *DetailEFF({ }, { put, call }) {
            Toast.loading('加载中...', 3);
            const result = yield call(meberDetailApi);
            if (result.code == 200) {
                yield put({ type: 'save', data: {dataList:result.ret||[]} });
                Toast.hide();
            } else {

            }
        },
        *OrderTrackEFF({ obj }, { put, call }) {
            Toast.loading('加载中...', 3);
            const result = yield call(OrderTrackAPi,obj);
            if (result.code == 200) {
                yield put({ type: 'save', data: {OrderTrack:result.ret||[]} });
                Toast.hide();
            } else {

            }
        },

        *GetLogistics({ obj }, { put, call, select }) {
            const dataList = yield select(state => state.member.logisticsList);
            const result = yield call(logisticsAPi, obj)
            if (result.code == 200) {
                yield put({ type: 'save', data: {logisticsData: result || {}} });
                if (result.pageNo > 1) yield put({ type: 'save', data: {logisticsList: [...dataList, ...result.ret.logisticsList]} });
                else yield put({ type: 'save', data: {logisticsList: result.ret.logisticsList || []} });
                Toast.hide();
            } else {

            }
            return result
        },

    },
    subscriptions: {
        setup({ dispatch, history }) {
          return history.listen(({ pathname, query }) => {
             // console.log(pathname)
            if (pathname == '/messagesCenter') {
                dispatch({ type: 'GetLogistics', obj: {pageNo: 1}});

            }
         });

      },
    },
};
